Decoding Nikon NEF Files
NEF is the file format Nikon cameras use to store RAW pictures.
While they pretend to be a fancy special file format, in reality they are mostly just TIFF with some custom tags.
Try opening a NEF file in a document viewer that supports TIFF files; It will most likely display a low resolution thumbnail of the image (embedded in the file) even if it has absolutely no idea what to do with the RAW data. TIFF is cool that way, extensibility is baked in. A shame Nikon had to ruin the fun by pretending its a custom file type.
If you want to decode NEF files, you first need to understand how to parse TIFF files; You should know what IFDs and sub-IFDs are and how to access them, what dir-entry tags and types are and how to read them, how strips work, and so on. Luckily, TIFF is almost trivial to parse, just make sure you don't mess up the endianess.
The special tags Nikon added to TIFF are reasonably well↗ documented↗.
The NEF files generated by my D60 have one main IFD, which contains an RGB bitmap thumbnail of the image, and two sub-IFDs, the first of which contains the RAW pixel data, the second one contains a JPEG version of the image. There is also an EXIF block. Some cameras create a special MakerNote block too, but my D60 appearently does not.
Other cameras might produce other IFD layouts. The IFD with the RAW pixel data should have tag verbatim "Photometric Interpretation" set to verbatim "Colour Filter Array". Additionally tag verbatim "New Subfile Type" will have bit 0 "reduced resolution version" set for the IFDs of the thumbnail and JPEG, but not for IFD of the RAW data.
There are multiple different ways the raw data might be encoded. Tag verbatim "Bit per Sample" will be either 12 or 14. Tag verbatim "Compression" will be either verbatim "uncrompressed" or verbatim for nikons own weird compression scheme. Unlike the nikon-specific tags, this compression scheme is hard to read up on; barely any sources exist. Some parts of the image are even encrypted↗.
The raw pixels may be stored as a thing called "JPEG lossless"?↗
I had to resort to reading the source code of dcraw↗, a CLI raw decoding program, to get anywhere. Dcraw is "free for all uses" if given credit and a decent tool itself.